<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 * Others Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Others extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->language('others');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('others_model');
			$this->set_controller('others');
			$this->set_model($this->others_model);
		}
                $this->load->model('bookhistory_model');
                $this->load->library('bookinglib');
                $this->booking = new bookinglib();
	}
        
        public function index(){
            redirect('auth/others/quickprint/');
        }


        private function menuTop(){
            $this->load->model('stats_model');
            $user_info                          = $this->stats_model->get_user_info($this->stats_model->get_user_id());
            $this->view_data['role_by_group']   = $user_info['role_list'];
            $this->view_data['username']        = $user_info['username'];
            return $this->view_data;
        }
        
        public function quickprintTemplate(){
            $this->menuTop();
            $this->load->model('configuration_model');
            $this->view_data['configuration']                 = $this->configuration_model->find_by();
            if($this->input->server('REQUEST_METHOD')=='POST'){
                
                    $tmp        = $this->input->post('booking-info');
                    $a          = explode("-",$tmp[0]);
                    $info       = array(
                        'booking_place_code'    =>  $this->input->post('booking_place_code'),
                        'sumMoney_fake'         =>  ($this->input->post('sumMoney_fake'))?$this->input->post('sumMoney_fake'):'',
                        'booking_customer_name' =>  array_chunk(explode("-", $this->input->post('customersName_fake')),2),
                        'booking'               =>  $this->input->post('booking-info'),
                        'dkve'                  =>  ($this->input->post('dkve'))?$this->input->post('dkve'):'',
                        'dkhanhly'              =>  ($this->input->post('dkhanhly'))?$this->input->post('dkhanhly'):'',
                        'condition_print'       =>  $this->input->post('condition_print'),
                        'logo'                  =>  substr($a[1],0,2)
                    );
                    $this->view_data['css']			= array(
                                        STATIC_URL.'backend/css/page.css'
                    );
                    $this->view_data['info']        = $info;
                    $this->load->view('auth/others/quickprintTemplate', $this->view_data);
            } else {
                    redirect('auth/others/quickprint');
            }
            
        }


        public function quickprint(){
            $this->menuTop();
            $this->load->model('configuration_model');
            $this->view_data['configuration']                 = $this->configuration_model->find_by();
            
            $this->load->view('auth/others/quickprint', $this->view_data);
            
            
        }
        
        public function quickPrintReceipt(){
            $this->menuTop();
            $this->load->model('configuration_model');
            $this->view_data['configuration']                 = $this->configuration_model->find_by();
            
            $this->load->view('auth/others/quickPrintReceipt', $this->view_data);
        }
        
        public function quickPrintReceiptTemplate(){            
            if($this->input->server('REQUEST_METHOD')=='POST'){
                    $this->load->view('auth/others/quickPrintReceiptTemplate', $this->view_data);
            } else {
                    redirect('auth/others/quickprint');
            }
            
        }
        
        public function receipt(){
            $this->bookinglib->direct();
            $this->load->model('configuration_model');
            $info       =   array(
                'pnr'               =>      $this->input->get('pnr'),
                'name'              =>      $this->input->get('name'),
                'address'           =>      $this->input->get('address'),
                'sale_fare'         =>      $this->input->get('sale_fare'),
                'booker'            =>      $this->input->get('booker')
            );
            $this->view_data['css']			= array(
				STATIC_URL.'backend/css/page.css',
		);
            
            $this->view_data['info']                          = $info;
            $this->view_data['configuration']                 = $this->configuration_model->find_by();
            
            $this->load->view('auth/others/receipt', $this->view_data);
        }


        public function checkPnr(){
            if($this->input->server('REQUEST_METHOD')=='POST'){
                $booking_info = $this->others_model->getInfoPnrTicket($this->input->post('booking_place_code'));
                if(empty($booking_info)){
                    die("");
                } else {
                    echo json_encode($booking_info);exit;
                }
            }
        }
}